package com.yanqu.road.server.logic.recharge.limit.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;

public class SystemOpenTriggerActivityMallLimit extends BaseActivityMallLimit{
    public SystemOpenTriggerActivityMallLimit(long userId, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(userId, mallInfo, activityInfo);
    }

    @Override
    protected int activityCheck() {
        if(!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if (activityInfo.getType() != eActivityType.SystemOpenRMBMallActivity.getValue()) {
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        return 0;
    }

    @Override
    protected boolean isSystemUnLock() {
        return true;
    }

    @Override
    protected LimitBuyResult buyCountLimit() {
        LimitBuyResult limitBuyResult = new LimitBuyResult();
        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
        if (null == baseActivityData) {
            limitBuyResult.setRet(GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY);
            return limitBuyResult;
        }
        long todayZeroTime = DateHelper.getTodayZeroTime();
        List<UserActivityConditionData> conditionDataList = baseActivityData.getUserActivityConditionDataList();
        for (UserActivityConditionData userActivityConditionData : conditionDataList) {
            if (userActivityConditionData.getValue().intValue() == mallInfo.getMallId()) {
                if (Long.parseLong(userActivityConditionData.getParam()) <= System.currentTimeMillis() / 1000) {//超过触发时间
                    limitBuyResult.setRet(GameErrorCode.E_MALL_LIMIT_BUY);
                    return limitBuyResult;
                }
                // 判断今日限购
                ActivityMallModule activityMallModule = player.getModule(ActivityMallModule.class);
                int limitCount = activityMallModule.getLimitCount(mallInfo);
                if (todayZeroTime <= userActivityConditionData.getModifyTime() && limitCount <= 0) {
                    limitBuyResult.setRet(GameErrorCode.E_MALL_LIMIT_BUY);
                    return limitBuyResult;
                }
                limitBuyResult.setLimitCount(limitCount);
                return limitBuyResult;
            }
        }
        limitBuyResult.setRet(GameErrorCode.E_MALL_LIMIT_BUY);
        return limitBuyResult;
    }

    @Override
    protected int logicCheck() {
        return 0;
    }
}
